package com.htgd.gardenexpo.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.dto.BindingBraceletDTO;
import com.htgd.gardenexpo.dto.CompetitionDTO;
import com.htgd.gardenexpo.dto.GeConUserCompeteDTO;
import com.htgd.gardenexpo.dto.PostSportsDataDTO;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConUserCompete;
import com.htgd.gardenexpo.page.CompeteQuery;
import com.htgd.gardenexpo.page.CompeteUserQuery;
import com.htgd.gardenexpo.page.SportQuery;
import com.htgd.gardenexpo.service.GeConUserCompeteService;
import com.htgd.gardenexpo.utils.TokenUtil;
import com.htgd.gardenexpo.vo.GeConSportVO;
import com.htgd.gardenexpo.vo.GeConUserCompeteVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * @author: 王秀田
 * @date: 2024/11/29 11:46
 * @description: 参赛人员管理
 */
@RestController
@RequestMapping("/api/compete")
@Api(tags = "参赛人员")
@Slf4j
public class GeConUserCompeteController {

    @Autowired
    private GeConUserCompeteService geConUserCompeteService;

    @GetMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<HashMap>> list(CompeteQuery competeQuery) {
        CommonResult<List<HashMap>> result = new CommonResult<>();
        try {
            PageInfo<HashMap> list = geConUserCompeteService.pageList(competeQuery);
            result.setCode(200);
            result.setMessage(String.valueOf(list.getTotal()));
            result.setData(list.getList());
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/listNew")
    @ApiOperation("参赛人员查询")
    public CommonResult<List<GeConUserCompeteVO>> listNew(CompeteUserQuery competeUserQuery) {
        log.info("参赛人员-参赛人员查询：{}",competeUserQuery);
        PageInfo<GeConUserCompeteVO> list = geConUserCompeteService.listNew(competeUserQuery);
        return CommonResult.success(String.valueOf(list.getTotal()),list.getList());
    }


    @PostMapping(value = "/add")
    @ApiOperation("参赛人员报名")
    public CommonResult add(@RequestBody GeConUserCompeteDTO geConUserCompete,HttpServletRequest request) {
        log.info("参赛人员报名：{},{}",geConUserCompete,request);
        return geConUserCompeteService.add(geConUserCompete,request);
    }


    @PostMapping(value = "/start")
    @ApiOperation("开始比赛")
    public CommonResult<JSONObject> startCompetition(@RequestBody CompetitionDTO competition,HttpServletRequest request) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConUserCompeteService.startCompetition(competition,request);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
            result.setMessage("失败");
        }
        result.setMessage("成功");
        result.setCode(200);
        return result;
    }

    @PostMapping(value = "/end")
    @ApiOperation("结束比赛")
    public CommonResult<JSONObject> endContest(@RequestBody CompetitionDTO competition,HttpServletRequest request) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConUserCompeteService.endContest(competition,request);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
            result.setMessage("失败");
        }
        result.setMessage("成功");
        result.setCode(200);
        return result;
    }

    @PostMapping(value = "/cancel")
    @ApiOperation("取消报名")
    public CommonResult<JSONObject> cancel(@RequestBody CompetitionDTO competition,HttpServletRequest request) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            String accessToken = request.getHeader("Access_token");
            String openId = TokenUtil.getUsernameFromToken(accessToken);
            GeConUserCompete geConUserCompete = new GeConUserCompete();
            geConUserCompete.setStatus("2");
            geConUserCompete.setMotionId(competition.getRaceId());
            geConUserCompete.setOpenId(openId);
            geConUserCompete.setUserId(Long.valueOf(competition.getUserId()));
            geConUserCompeteService.updateUserCompete(geConUserCompete);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
            result.setMessage("失败");
        }
        result.setMessage("成功");
        result.setCode(200);
        return result;
    }

    @PostMapping(value = "/bindingBracelet")
    @ApiOperation("绑定手环")
    public CommonResult bindingBracelet(@RequestBody BindingBraceletDTO bindingBraceletDTO, HttpServletRequest request) {
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        log.info("绑定手环：{}",bindingBraceletDTO);
        return geConUserCompeteService.bindingBracelet(bindingBraceletDTO);
    }

    @GetMapping(value = "/queryScores")
    @ApiOperation("查询成绩")
    public CommonResult queryScores(String id) {
        log.info("查询成绩：{}",id);
        return geConUserCompeteService.queryScores(id);
    }


    @PostMapping(value = "/postSportsData")
    @ApiOperation("甲板推送给小程序赛事数据")
    public CommonResult postSportsData(@RequestBody PostSportsDataDTO postSportsDataDTO) {
        log.info("甲板推送给小程序赛事数据");
        return geConUserCompeteService.postSportsData(postSportsDataDTO);
    }

}
